'use strict';
// Connect to socket server
const socket = io();
// Create RTC Peer connection object
const peer = new RTCPeerConnection({
  iceServers: [
    { urls: 'stun:stun.internetcalls.com' }
  ],
});

peer.onicecandidate = function (event) {
  if (event.candidate) {
    const ip = event.candidate.candidate.split(' ')[4];
    console.log('admin=>Local IP Address:', ip);
    peer.onicecandidate = null;
  }
};
// Listen to track event
const video = document.getElementById('client-screen');
peer.addEventListener('track', (track) => {
  // display client screen shared
  video.srcObject = track.streams[0];
});

// listen to `offer` event from client (actually from server)
socket.on('offer', async (clientSDP) => {
  await peer.setRemoteDescription(clientSDP);

  // create an answer and send the answer to client
  const sdp = await peer.createAnswer();
  await peer.setLocalDescription(sdp);
  socket.emit('answer', peer.localDescription);
  // console.log(`emit[answer]=>${JSON.stringify(peer.localDescription, null, 2)}`);

});

/** Exchange ice candidate */
peer.addEventListener('icecandidate', (event) => {
  if (event.candidate) {
    // send the candidate to client
    socket.emit('icecandidate', event.candidate);
    console.log(`admin=>emit[icecandidate]=>${JSON.stringify(event.candidate, null, 2)}`);

  }
});
socket.on('icecandidate', async (candidate) => {
  // get candidate from client
  await peer.addIceCandidate(new RTCIceCandidate(candidate));
  console.log(`admin=>on[icecandidate]=>${JSON.stringify(candidate, null, 2)}`);
});
